package com.iatinspur.dubbo.factory;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;

import java.util.HashMap;
/**
 * 后面的测试 可以用这个
 */
public class GenericServiceFactory {
    /**
     *
     * @param clzss
     * @param version
     * @param protocol
     * @return
     * @param <T>
     */
    public  static <T> T getNacosService(Class<T>clzss,String version,String protocol){
        ApplicationConfig config=new ApplicationConfig("consumer-do-provider");
        ReferenceConfig<T> reference=new ReferenceConfig<>();
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("nacos://127.0.0.1:8848");
        registry.setUsername("nacos");
        registry.setPassword("nacos");
        HashMap<String,String> map=new HashMap<>();
        map.put("namespace", "d3883ff7-0010-456e-bae2-01e37fc114f4");
        map.put("group", "dubbo3.x");
        registry.setRegister(false);
        registry.setParameters(map);
        reference.setInterface(clzss);
        reference.setVersion(version);
        if(StringUtils.isNotBlank(protocol)){
            reference.setProtocol(protocol);
        }
        reference.setRegistry(registry);
        reference.setApplication(config);
        reference.setAsync(true);
//        reference.setGeneric(true);
        return reference.get();
    }


    public  static <T> T getZookeeperService(Class<T>clzss,String version,String protocol){
        ApplicationConfig config=new ApplicationConfig("consumer-do-provider");
        ReferenceConfig<T> reference=new ReferenceConfig<>();
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");
        registry.setRegister(false);
        reference.setInterface(clzss);
        reference.setVersion("1.0.0");
        reference.setProtocol("dubbo");
        reference.setRegistry(registry);
        reference.setApplication(config);
        reference.setAsync(true);
        if(StringUtils.isNotBlank(protocol)){
            reference.setProtocol(protocol);
        }
        return reference.get();
    }


    public  static <T> T getDubboService(Class<T>clzss,String version,String protocol,Integer port){
        ApplicationConfig config=new ApplicationConfig("consumer-do-provider");
        ReferenceConfig<T> reference=new ReferenceConfig<>();
        reference.setInterface(clzss);
        reference.setVersion(version);
        reference.setUrl("dubbo://127.0.0.1:"+port);
        if(StringUtils.isNotBlank(protocol)){
            reference.setProtocol(protocol);
        }
        reference.setApplication(config);
        reference.setAsync(true);
   //     reference.setGeneric(true);
        return reference.get();
    }



}
